/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tarifador.view;

import br.com.tarifador.controller.AgendaRN;
import br.com.tarifador.dataModel.AgendaDataModel;
import br.com.tarifador.model.Agenda;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.HibernateException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author charles
 */
@ManagedBean
@SessionScoped
public class AgendaBean {

    private AgendaRN agendaRN = new AgendaRN();
    private List<Agenda> listaContatos;
    private AgendaDataModel agendaModel;
    private Agenda agendaSelect;
    private List<Agenda> filterAgenda;

    public void salvaAgenda() {

        boolean salvar = false;
        try {
            salvar = agendaRN.salvaAgenda();
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (salvar == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaAgenda();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void listaAgenda() {
        listaContatos = new ArrayList<Agenda>();
        try {
            listaContatos = agendaRN.getListaDeContatos();

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        this.agendaModel = new AgendaDataModel(listaContatos);
    }

    public void alteraAgenda() {
        boolean valida = false;
        try {
            valida = agendaRN.updateAgenda(agendaSelect);

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaAgenda();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));

        }
    }

    public void deleteAgenda() {
        boolean valida = false;

        try {
            valida = agendaRN.deletaAgenda(agendaSelect.getId());
        } catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo! " + e.getMessage(), ""));
        } catch (HibernateException e) {
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao excluir dados! " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao excluir!", ""));
        } else {
            listaAgenda();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void btnAlterarAgenda() {
        RequestContext request = RequestContext.getCurrentInstance();
        try {
            if (agendaSelect.getId() != null && agendaSelect.getId() > 0) {
                request.addCallbackParam("sucesso", true);
            } else {
                request.addCallbackParam("sucesso", false);
            }
        } catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo!", ""));
        }
    }

    public void limpaLista() {
        if (agendaModel != null) {
            listaContatos.clear();
            agendaModel = new AgendaDataModel(listaContatos);
        }
    }

    public AgendaRN getAgendaRN() {
        return agendaRN;
    }

    public void setAgendaRN(AgendaRN agendaRN) {
        this.agendaRN = agendaRN;
    }

    public List<Agenda> getListaContatos() {
        return listaContatos;
    }

    public void setListaContatos(List<Agenda> listaContatos) {
        this.listaContatos = listaContatos;
    }

    public AgendaDataModel getAgendaModel() {
        return agendaModel;
    }

    public void setAgendaModel(AgendaDataModel agendaModel) {
        this.agendaModel = agendaModel;
    }

    public Agenda getAgendaSelect() {
        return agendaSelect;
    }

    public void setAgendaSelect(Agenda agendaSelect) {
        this.agendaSelect = agendaSelect;
    }

    public List<Agenda> getFilterAgenda() {
        return filterAgenda;
    }

    public void setFilterAgenda(List<Agenda> filterAgenda) {
        this.filterAgenda = filterAgenda;
    }
}
